﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;

namespace baseClass.controls
{
    public partial class employee_general : UserControl
    {
        private Color saveNormalFC = application.Settings.sysColorNormalFG;
        public employee_general()
        {
            InitializeComponent();
            saveNormalFC = codeEd.ForeColor;
            SetMaxLength();
            codeEd.isToUpperCase = true;
            imageLbl.Text = "(" + emImage.Width.ToString()+"x"+emImage.Height.ToString()+")";
        }
        private void SetMaxLength()
        {
            data.baseDS.employeeDataTable tbl = new data.baseDS.employeeDataTable();
            codeEd.maxlen = tbl.emCodeColumn.MaxLength;
            fullNameEd.maxlen = tbl.fullNameColumn.MaxLength;
            addressEd1.maxlen = tbl.address1Column.MaxLength;
            addressEd1.maxlen = tbl.address2Column.MaxLength;
            phoneEd.maxlen = tbl.phone1Column.MaxLength;
            mobileEd.maxlen = tbl.mobileColumn.MaxLength;
            emailEd.MaxLength = tbl.email1Column.MaxLength;
        }

        public virtual void LoadData() 
        {
            sexCb.LoadData();
        }
        public virtual void SetData(data.baseDS.employeeRow row)
        {
            codeEd.Text = row.emCode;
            fullNameEd.Text = row.fullName;
            if (!row.IsemImageNull()) emImage.Image = common.images.Byte2Image(row.emImage);
            else emImage.Image = defaultEmImg.Image;

            sexCb.myValue = (application.myTypes.sex)row.sex;
            addressEd1.Text = row.address1;
            addressEd2.Text = (row.Isaddress2Null()?"":row.address2);
            phoneEd.Text = (row.Isphone1Null() ? "" : row.phone1);
            mobileEd.Text = (row.IsmobileNull() ? "" : row.mobile);
            emailEd.Text = (row.Isemail1Null() ? "" : row.email1);
        }
        public virtual void GetData(data.baseDS.employeeRow row)
        {
            row.emCode = codeEd.Text;
            row.emImage = common.images.Image2Bytes(emImage.Image);
            row.fullName = fullNameEd.Text;
            row.sex = (byte)sexCb.myValue;
            row.address1 = addressEd1.Text;
            row.address1  = addressEd1.Text.Trim();
            if (addressEd2.Text.Trim() == "") row.Setaddress2Null(); else row.address2 = addressEd2.Text.Trim();
            if (phoneEd.Text.Trim() == "") row.Setphone1Null(); else row.phone1 = phoneEd.Text.Trim();
            if (mobileEd.Text.Trim() == "") row.SetmobileNull(); else row.mobile =mobileEd.Text.Trim();
            if (emailEd.Text.Trim() == "") row.Setemail1Null(); else row.email1 = emailEd.Text.Trim();
        }
        public virtual bool CheckData()
        {
            bool retVal = true;
            codeEd.ForeColor = saveNormalFC;
            fullNameLbl.ForeColor = saveNormalFC;
            sexLbl.ForeColor = saveNormalFC;
            addressLbl1.ForeColor = saveNormalFC;

            if (codeEd.Text.Trim() == "")
            {
                codeLbl.ForeColor = application.Settings.sysColorHiLightFG;
                retVal = false;
            }
            if (fullNameEd.Text.Trim() == "") 
            {
                fullNameLbl.ForeColor = application.Settings.sysColorHiLightFG;
                retVal = false;
            }
            if (sexCb.myValue == application.myTypes.sex.None) 
            {
                sexLbl.ForeColor = application.Settings.sysColorHiLightFG;
                retVal = false;
            }
            if (addressEd1.Text.Trim() == "") 
            {
                addressLbl1.ForeColor = application.Settings.sysColorHiLightFG;
                retVal = false;
            }
            return retVal;
        }
        public virtual void LockEdit(bool state)
        {
            codeEd.ReadOnly = state;
            fullNameEd.ReadOnly = state;
            addressEd1.ReadOnly = state;
            addressEd2.ReadOnly = state;
            phoneEd.ReadOnly = state;
            mobileEd.ReadOnly = state;
            emailEd.ReadOnly = state;

            sexCb.Enabled = !state;
            imLoadBtn.Enabled = !state;
        }
        public virtual void SetFocus()
        {
            this.Focus();
            codeEd.Focus();
        }

        private void imageBtn_Click(object sender, EventArgs e)
        {
            emImage.Image = common.images.GetImage();
        }
    }
}
